<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 左边的按钮 -->
        <div>
          <el-button type="primary" round @click="addPermissionBtn(1, '0', 'add')">添加权限</el-button>
          <el-button type="primary" round @click="test">测试动态添加路由</el-button>
        </div>
      </el-card>
      <!-- 写一个表格 -->
      <el-card>
        <el-table
          :data="list"
          row-key="id"
          style="width: 100%"
        >
          <el-table-column
            prop="name"
            label="名称"
            width="180"
          />
          <el-table-column
            prop="code"
            label="标识"
            width="180"
          />
          <el-table-column
            prop="description"
            label="描述"
          />
          <el-table-column
            fixed="right"
            label="操作"
          >
            <template slot-scope="scope">
              <el-button v-if="scope.row.type === 1" type="text" size="small" @click="addPermissionBtn(2, scope.row.id, 'add')">添加</el-button>
              <el-button type="text" size="small" @click="addPermissionBtn(scope.row.type, scope.row.id, 'edit')">编辑</el-button>
              <el-button v-if="scope.row.children.length === 0" type="text" size="small" @click="deletePermissionBtn(scope.row.id, scope)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit ? '编辑权限' : '新增权限'"
      :visible.sync="dialogVisible"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      @close="dialogClose"
    >
      <el-form ref="formRef" :model="form" label-width="80px">
        <el-form-item label="权限名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-switch v-model="form.enVisible" active-value="1" inactive-value="2" active-text="启用" inactive-text="不启用" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">{{ isEdit ? '更新' : '新增' }}</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionRequest, addPermissionRequest, deletePermissionByIdRequest, getPermissionByIdRequest, updatePermissionByIdRequest } from '@/api/permission'
import { transformListToTree2 } from '@/utils'
export default {
  data() {
    return {
      list: [],
      dialogVisible: false,
      form: {
        enVisible: '',
        name: '',
        code: '',
        description: '',
        type: 0,
        pid: ''
      },
      isEdit: false
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    test() {
      this.$router.addRoutes([
        {
          path: '/demo',
          component: () => import('@/views/demo.vue')
        },
        {
          path: '/test',
          component: () => import('@/views/demo.vue')
        }
      ])
    },
    async getPermission() {
      try {
        const res = await getPermissionRequest()
        if (res.success) {
          this.list = transformListToTree2(res.data, '0')
        } else {
          this.$message.error('获取权限接口失败')
        }
      } catch (err) {
        console.log('getPermissionRequest: ', err)
      }
    },
    async getPermissionById(pid) {
      try {
        const res = await getPermissionByIdRequest(pid)
        if (res.success) {
          this.$message.success(res.message)
          this.form = res.data
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('getPermissionByIdRequest: ', err)
      }
    },
    addPermissionBtn(type, pid, isEditAdd) {
      if (isEditAdd === 'add') {
        this.isEdit = false
      } else {
        // 为了回显，就发一个请求，将form对象重新赋值
        this.getPermissionById(pid)
        this.isEdit = true
      }
      this.dialogVisible = true
      this.form.type = type
      this.form.pid = pid
    },
    async addPermission() {
      try {
        const res = await addPermissionRequest(this.form)
        if (res.success) {
          console.log('addPermissionRequest: ', res)
          this.dialogVisible = false
          this.getPermission()
        } else {
          this.$message.error('新增权限失败')
        }
      } catch (err) {
        console.log('addPermissionRequest: ', err)
      }
    },
    async updatePermissionById() {
      try {
        const res = await updatePermissionByIdRequest(this.form)
        this.dialogVisible = false
        this.getPermission()
        if (res.success) {
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('updatePermissionByIdRequest: ', err)
      }
    },
    onSubmit() {
      if (this.isEdit) {
        this.updatePermissionById()
      } else {
        this.addPermission()
      }
    },
    async deletePermissionById(id) {
      try {
        const res = await deletePermissionByIdRequest(id)
        if (res.success) {
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('deletePermissionByIdRequest: ', err)
      }
    },
    deletePermissionBtn(id, scope) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deletePermissionById(id, scope)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    dialogClose() {
      console.log(111)
      // 1. 重置表单的校验， 2. 初始化表单form的值
      this.$refs.formRef.resetFields()
      // this.$options 这个东西是vue是初始化这个组件的时候的 初始配置对象
      this.form = this.$options.data().form
    }
  }
}
</script>
